package com.easysoft.framework.article.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.article.mapper.SysProblemMapperExtd;
import com.easysoft.framework.article.model.SysProblem;
import com.easysoft.framework.article.model.SysProblemQuery;
import com.easysoft.framework.article.model.SysProblemView;
import com.easysoft.framework.beans.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.ProblemService")
public class ProblemService {
    @Autowired
    @Qualifier("mapper.SysProblemMapperExtd")
    private SysProblemMapperExtd sysproblemMapperExtd;

    @DataSource(DataSource.SLAVE)
    public List<SysProblemView> selectSysProblemByPage(Page page, SysProblemQuery query){
        int maxRow = sysproblemMapperExtd.selectCountAllProblem(query);
        if (maxRow == 0){
            return new ArrayList<SysProblemView>();
        }
        page.setMaxRow(maxRow);
        return sysproblemMapperExtd.selectSysProblemByPage(page,query);
    }

    @DataSource(DataSource.MASTER)
    public int addSysProblem(SysProblem problem){
        problem.setId(UUIDUtils.getUUID());
        problem.setCreateTime(new Date());
        return sysproblemMapperExtd.insertSelective(problem);
    }

    @DataSource(DataSource.SLAVE)
    public SysProblem selectSysProblemById(String id){
        return sysproblemMapperExtd.selectByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysProblem(String id){
        return sysproblemMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysProblem(SysProblem problem){
        return sysproblemMapperExtd.updateByPrimaryKeySelective(problem);
    }
}
